package sch.jl.caigou.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jiliang.purchase.demo.BusinessProcessing;
import jiliang.purchase.demo.CriteriaBusinessProcess;
import sch.jl.caigou.dao.BusinessProcessingDao;
import sch.jl.caigou.utils.Page;
import sch.jl.caigou.utils.getMax;

/*
 * 处理业务
 * js_id 角色ID
 * CBP 限制条件
 * **/
public class BusinessProcessImpl extends DaoImpl<BusinessProcessing>
	implements BusinessProcessingDao
	
{
	public Page<BusinessProcessing> getInfo(String js_id,String name,CriteriaBusinessProcess CBP) {
		// TODO Auto-generated method stub		
		Page<BusinessProcessing> page = new Page<BusinessProcessing>();
		List<BusinessProcessing> list = new ArrayList<BusinessProcessing>();
		list = getList(js_id,name,CBP);
		
		
		//System.out.println(list.size());
		page.setTotalItemNumber(list.size());

		page.setPageNo(CBP.getPageNum());
		page.setList(list);
		return  page;
	}
	
	
	public List<BusinessProcessing> getPdf(String name,CriteriaBusinessProcess CBP)
	{
		getMax g  = new getMax();
		
		try{
			String sqs = g.getShenhe(name);
			List<BusinessProcessing> list = new ArrayList<BusinessProcessing>();
			
			String sql = "select think_shenqing.bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states,caigoufs from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm,think_lingxing_sbjj " +
			"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id and think_shenqing.bh = think_lingxing_sbjj.bh  and think_shenqing.sq_id in ("+sqs+")";
			
			sql = getSQL(sql,CBP);
			
			//System.out.println(sql);
			
			list =  queryForList(sql);
			for(int i =0;i<list.size();i++)
			{
				list.get(i).setSign("1");
			}
			return list;
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return null;
	}
	
	public List<BusinessProcessing> getSPec(CriteriaBusinessProcess CBP)
	{
		List<BusinessProcessing> list = new ArrayList<BusinessProcessing>();
		String sql = "select think_shenqing.bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states,caigoufs from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm,think_lingxing_sbjj " +
		"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and last_lc = '计财处审核通过'  and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id and think_shenqing.bh = think_lingxing_sbjj.bh and states ='3' ";		
		
		sql = getSQL(sql,CBP);
		
		list = queryForList(sql);
		
		System.out.println(sql);
		
		for(int i =0;i<list.size();i++)
		{
			list.get(i).setSign("1");
		}
		
		return list;
	}
	
	public List<BusinessProcessing> getPdf2(String name,CriteriaBusinessProcess CBP,String js_id)
	{
		List<BusinessProcessing> list = new ArrayList<BusinessProcessing>();
		String sql = "select think_shenqing.bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states,allca_name,caigoufs from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm,think_lingxing_sbjj,think_allcateshenqing " +
		"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and last_lc = '计财处审核通过'  and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id and think_shenqing.bh = think_lingxing_sbjj.bh and states ='7' and think_shenqing.bh = allca_bh and allca_name='"+name+"' ";
		
		if(js_id.contains(",20,"))
		{
			sql = "select think_shenqing.bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states,allca_name,caigoufs from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm,think_lingxing_sbjj,think_allcateshenqing " +
			"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and last_lc = '计财处审核通过'  and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id and think_shenqing.bh = think_lingxing_sbjj.bh and states ='7' and think_shenqing.bh = allca_bh  ";
		}
		
		sql = getSQL(sql,CBP);
//		System.out.println(sql);
		list =  queryForList(sql);
		return list;
	}
	
	public List<BusinessProcessing> getPdf3(String name,CriteriaBusinessProcess CBP,String js_id)
	{
		List<BusinessProcessing> list = new ArrayList<BusinessProcessing>();
		
		String sql = "select bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states,caigoufs from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm " +
		"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id and states='8' ";
		
		sql = getSQL(sql,CBP);
		//System.out.println(sql);
		list =  queryForList(sql);
		return list;
	}

		
	public List<BusinessProcessing> getList(String js_id,String name,CriteriaBusinessProcess CBP)
	{
		
		String sql = "";
		if(js_id.contains(",20,"))
		{
			sql = "select think_shenqing.bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states,caigoufs from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm,think_lingxing_sbjj " +
					"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id and states!='7' and think_shenqing.bh = think_lingxing_sbjj.bh ";
			//System.out.println(sql);
		}
		else if(js_id.contains(",8,"))
		{
			sql = "select think_shenqing.bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm,think_lingxing_sbjj " +
					"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and last_lc = '申请人提交申请' and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id and think_shenqing.bh = think_lingxing_sbjj.bh and think_lingxing_sbjj.fuzerenxm = '"+name+"' and think_shenqing.states='1' ";
			//System.out.println(sql);
		}
		else if(js_id.contains(",3,"))
		{
			sql = "select bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm " +
			"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and last_lc = '经费负责人审核通过' and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id ";
			//System.out.println(sql);

		}
		else if(js_id	.equals(",10,"))
		{
			sql = "select bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm " +
			"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and last_lc = '实验处审核通过' and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id";
			//System.out.println(sql);
		}
		else if(js_id.equals(",15,"))
		{
			sql = "select bh,xm_mingcheng,xingming,sq_shijian,last_lc,bz_mc,states from think_shenqing,think_shengqinglc,think_shenqingbz,think_shenqingxm " +
			"where think_shenqing.lc_id = think_shengqinglc.lc_id and think_shenqing.xm_id='4' and last_lc = '采购中心审核通过' and think_shenqingbz.bz_id = think_shengqinglc.bz_id and think_shenqingxm.xm_id = think_shenqing.xm_id";
			//System.out.println(sql);
		}
		else if(js_id.contains(",1,"))
		{
			List<BusinessProcessing> list = new ArrayList<BusinessProcessing>();
			return list;
		}
		sql = getSQL(sql,CBP);
		//System.out.println(sql);
		//list = queryForList(sql);	
		return queryForList(sql);
		
		
	}
	
	private String getSQL(String sql,CriteriaBusinessProcess CBP)
	{
		String querySql = sql;
	/*	if(CBP.getBh()==null & CBP.getLc_id()==null & CBP.getShenqingren()==null & CBP.getFrom_time()==null & CBP.getTo_time()==null & CBP.getStates()==null)
		{
			querySql = sql;
		}*/
		if(CBP.getBh()!=null)
		{
			querySql += " and think_shenqing.bh like '%"+CBP.getBh()+"%' ";
		}
		if(CBP.getLc_id()!=null)
		{
			querySql += " and think_shenqing.xm_id = '"+CBP.getLc_id()+"'";
		}
		if(CBP.getShenqingren()!=null)
		{
			querySql += " and think_shenqing.xingming like '%"+CBP.getShenqingren()+"%' ";
		}
		if(CBP.getFrom_time()!=null)
		{
			querySql += " and think_shenqing.sq_shijian >= '"+CBP.getFrom_time()+"'";
		}
		if(CBP.getTo_time()!=null)
		{
			querySql += " and think_shenqing.sq_shijian <= '"+CBP.getTo_time()+"'";
		}
		if(CBP.getStates()!=null)
		{
			querySql += " and think_shenqing.states = '"+CBP.getStates()+"'";
		}
		
		querySql += " order by sq_shijian DESC";
		
		//System.out.println(querySql);
		return querySql;
	}
	
	public List<BusinessProcessing> getPageList(List<BusinessProcessing> list, int page)
	{
		List<BusinessProcessing> pageList = new ArrayList<BusinessProcessing>();
		if (list.size() == 0)
		{
			return pageList;
		}
		if (page * 10 >= list.size())
		{
			for (int i = (page - 1) * 10; i < list.size(); i++)
			{
				pageList.add(list.get(i));
			}
		} else
		{
			for (int i = 0; i < 10; i++)
			{
				pageList.add(list.get((page - 1)*10 + i));
			}
		}
		
		
		return pageList;
	}
	
}
